<?php
require_once("../config.php");
require_once("../Info.php");
require_once("../Database.php");
require_once("../Lineup.php");
require_once("Artist.php");
$lockfile = "../cache/proc.lck";

set_time_limit(9999);
ini_set("mysql.connect_timeout", 999);

$db = Database::getInstance();
$query = $db->query("SELECT * FROM cachequeue ORDER BY type DESC");

while($row = mysql_fetch_array($query)) {
	if($row['type'] == "artist.getsimilar") {
       $file = "../cache/artist/similar/".$row['name'].".serialized";

       if(!file_exists($file)) {
            $xmlUrl = "http://ws.audioscrobbler.com/2.0/?method=artist.getsimilar&artist=".$row['name']."&api_key=519681edb551a3b5177bb67352e16136&limit=150";
	        $sxml = simplexml_load_file($xmlUrl);
	        foreach($sxml->similarartists->artist as $obj) {
	            $artist = new Artist((string)$obj->name);
	            $artist->weight = (string)$obj->match;
	            $similarArtists[] = $artist;
	            $i++;
	        }
	        $fp = @fopen($file, "w");
	        if($fp) {
	            $result = fwrite($fp, serialize($similarArtists));
	            fclose($fp);
	        }
       }            
	} elseif($row['type'] == "geo.getevents") {
		//echo $row['type']." - inside";
    	Lineup::getEvents($row['name'], true);
    } else {
	   trigger_error("\$row['type'] not set"); die;
	}
    
	sleep(1); //prevent hammering the poor last.fm server
    
	//TODO: optimize query
	$db->query("DELETE FROM cachequeue WHERE name='".$row['name']." ' AND type='".$row['type']."'");
}
unlink($lockfile);

?>